import matplotlib.pyplot as plt
import pandas as pd
import re
from datetime import datetime

# 提取准确率的正则表达式
pattern = re.compile(r"Epoch \d+/\d+, loss: [\d.]+, accuracy: ([\d.]+)")

# 日志文件路径
log_file_path_ours = 'results/vgg16_adp_40g.log'
log_file_path_allreduce_40g = 'results/vgg16_allreduce_40g.log'
log_file_path_topk_40g = 'results/vgg16_topk_40g.log'
log_file_path_allreduce_1g = 'results/vgg16_allreduce_1g.log'
log_file_path_ours_1g = 'results/vgg16_adpt_1g.log'
log_file_path_topk_1g = 'results/vgg16_topk_1g.log'
# 初始化列表来存储时间和准确率
times = []

def extra(log_file_path):
    # 打开并读取日志文件
    accuracies = []
    times = []
    with open(log_file_path, 'r') as file:
        for line in file:
            # 提取时间戳
            time_match = re.search(r'\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}', line)
            if time_match:
                # 将时间转换为datetime对象
                timestamp = datetime.strptime(time_match.group(), '%Y-%m-%d %H:%M:%S')
                times.append(timestamp)

            # 提取准确率
            accuracy_match = pattern.search(line)
            if accuracy_match:
                accuracies.append(float(accuracy_match.group(1)))
    return accuracies, times
import matplotlib.pyplot as plt

# Assuming 'extra' function is defined somewhere else that extracts accuracy and time from logs
# Extracted data (x1, y1) and (x2, y2) represent two experiments with possibly different time points
adp_accs, adp_times = extra(log_file_path_ours)
allreduces_accs, allreduce_times = extra(log_file_path_allreduce_40g)
topk_accs, topk_times = extra(log_file_path_topk_40g)
allreduce_1g_accs, allreduce_1g_times = extra(log_file_path_allreduce_1g)
adp_1g_accs, adp_1g_times = extra(log_file_path_ours_1g)
topk_1g_accs, topk_1g_times = extra(log_file_path_topk_1g)
# 将时间转换为从第一个时间点开始的秒数
adp_times_in_seconds = [(t - adp_times[0]).total_seconds() for t in adp_times]
allreduce_times_in_seconds = [(t - allreduce_times[0]).total_seconds() for t in allreduce_times]
topk_times_in_seconds = [(t - topk_times[0]).total_seconds() for t in topk_times]
allreduce_times_1g_in_seconds = [(t - allreduce_1g_times[0]).total_seconds() for t in allreduce_1g_times]
adp_1g_times_in_seconds = [(t - adp_1g_times[0]).total_seconds() for t in adp_1g_times]
topk_1g_times_in_seconds = [(t - topk_1g_times[0]).total_seconds() for t in topk_1g_times]
# Plotting
plt.figure(figsize=(10, 4))

# Plot each dataset independently since they may have different x (time) values
plt.plot(adp_times_in_seconds, adp_accs, label='NetSenseML-40G', marker='^', markersize=8, linestyle='-', linewidth=2, color=(221/255, 159/255, 221/255))
plt.plot(allreduce_times_in_seconds, allreduces_accs, label='AllReduce-40G', marker='s', markersize=8, linestyle='-', linewidth=2, color=(64 / 255, 224/255, 208/255))
plt.plot(topk_times_in_seconds, topk_accs, label='TopK-0.01-40G', marker='o', markersize=8, linestyle='-', linewidth=2, color=(255/255, 222/255, 74/255))
plt.plot(allreduce_times_1g_in_seconds, allreduce_1g_accs, label='AllReduce-1G', marker='v', markersize=8, linestyle='-', linewidth=2, color=(112/255, 128/255, 143/255))
plt.plot(adp_1g_times_in_seconds, adp_1g_accs, label='NetSenseML-1G', marker='*', markersize=8, linestyle='-', linewidth=2, color='red')
# plt.plot(topk_1g_times_in_seconds, topk_1g_accs, label='TopK-0.01-1G', marker='1', markersize=8, linestyle='-', linewidth=2, color='green')


# Title and labels
plt.title('VGG16 (Accuracy)', fontsize=16)
plt.xlabel('Time (seconds)', fontsize=16)  # 已去除日期，只显示秒数
plt.ylabel('Achievable Accuracy (%)', fontsize=16)


# Horizontal line at 90%
plt.axhline(y=0.87, color='black', linestyle='--', linewidth=3)
plt.text(50, 0.9, 'y=87%', fontsize=12, color='black')

# Grid and legend
plt.grid(True, linestyle='--', linewidth=0.5)
plt.legend(loc='lower right', fontsize=16)

# Tight layout and show
plt.tight_layout()
plt.savefig('time_to_accuracy_plot_vgg.pdf', format='pdf', bbox_inches='tight')



import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.inset_locator import inset_axes

plt.figure(figsize=(10, 4))

# Plot each dataset independently since they may have different x (time) values
plt.plot(adp_times_in_seconds, adp_accs, label='NetSenseML-40G', marker='^', markersize=8, linestyle='-', linewidth=2, color=(221/255, 159/255, 221/255))
plt.plot(allreduce_times_in_seconds, allreduces_accs, label='AllReduce-40G', marker='s', markersize=8, linestyle='-', linewidth=2, color=(64 / 255, 224/255, 208/255))
plt.plot(topk_times_in_seconds, topk_accs, label='TopK-0.01-40G', marker='o', markersize=8, linestyle='-', linewidth=2, color=(255/255, 222/255, 74/255))
plt.plot(allreduce_times_1g_in_seconds, allreduce_1g_accs, label='AllReduce-1G', marker='v', markersize=8, linestyle='-', linewidth=2, color=(112/255, 128/255, 143/255))
plt.plot(adp_1g_times_in_seconds, adp_1g_accs, label='NetSenseML-1G', marker='*', markersize=8, linestyle='-', linewidth=2, color='red')

# Title and labels
plt.title('VGG16 (Accuracy)', fontsize=16)
plt.xlabel('Time (seconds)', fontsize=16)
plt.ylabel('Achievable Accuracy (%)', fontsize=16)

# Horizontal line at 87%
plt.axhline(y=0.87, color='black', linestyle='--', linewidth=3)
plt.text(50, 0.9, 'y=87%', fontsize=12, color='black')

# Grid and legend
plt.grid(True, linestyle='--', linewidth=0.5)
plt.legend(loc='lower right', fontsize=16)




import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec

# Create a figure with two subplots (main plot + zoom-in plot)
fig = plt.figure(figsize=(12, 8))
gs = GridSpec(2, 1, height_ratios=[3, 1])  # Allocate space for two plots

# First plot: Full accuracy plot
ax0 = plt.subplot(gs[0])
ax0.plot(adp_times_in_seconds, adp_accs, label='NetSenseML-40G', marker='^', markersize=8, linestyle='-', linewidth=2, color=(221/255, 159/255, 221/255))
ax0.plot(allreduce_times_in_seconds, allreduces_accs, label='AllReduce-40G', marker='s', markersize=8, linestyle='-', linewidth=2, color=(64 / 255, 224/255, 208/255))
ax0.plot(topk_times_in_seconds, topk_accs, label='TopK-0.01-40G', marker='o', markersize=8, linestyle='-', linewidth=2, color=(255/255, 222/255, 74/255))
ax0.plot(allreduce_times_1g_in_seconds, allreduce_1g_accs, label='AllReduce-1G', marker='v', markersize=8, linestyle='-', linewidth=2, color=(112/255, 128/255, 143/255))
ax0.plot(adp_1g_times_in_seconds, adp_1g_accs, label='NetSenseML-1G', marker='*', markersize=8, linestyle='-', linewidth=2, color='red')

# Add horizontal line at 87%
ax0.axhline(y=0.87, color='black', linestyle='--', linewidth=3)
ax0.text(50, 0.9, 'y=87%', fontsize=16, color='black')

# Title and labels for first plot
ax0.set_title('VGG16 (Accuracy)', fontsize=16)
ax0.set_xlabel('Time (seconds)', fontsize=16)
ax0.set_ylabel(' Achievable Accuracy (%)', fontsize=16)

# Grid and legend
ax0.grid(True, linestyle='--', linewidth=0.5)
ax0.legend(loc='lower right', fontsize=16)

# Second plot: Zoom-in region
ax1 = plt.subplot(gs[1])
ax1.plot(adp_times_in_seconds, adp_accs, label='NetSenseML-40G', marker='^', markersize=8, linestyle='-', linewidth=2, color=(221/255, 159/255, 221/255))
ax1.plot(allreduce_times_in_seconds, allreduces_accs, label='AllReduce-40G', marker='s', markersize=8, linestyle='-', linewidth=2, color=(64 / 255, 224/255, 208/255))
ax1.plot(topk_times_in_seconds, topk_accs, label='TopK-0.01-40G', marker='o', markersize=8, linestyle='-', linewidth=2, color=(255/255, 222/255, 74/255))
ax1.plot(allreduce_times_1g_in_seconds, allreduce_1g_accs, label='AllReduce-1G', marker='v', markersize=8, linestyle='-', linewidth=2, color=(112/255, 128/255, 143/255))
ax1.plot(adp_1g_times_in_seconds, adp_1g_accs, label='NetSenseML-1G', marker='*', markersize=8, linestyle='-', linewidth=2, color='red')

# Focus on a zoomed-in region (define your zoom limits)
ax1.set_xlim(400, 800)
ax1.set_ylim(0.7, 0.85)

# Title and labels for the zoom-in plot
ax1.set_title('VGG16 (Accuracy) (zoom-in)', fontsize=16)
ax1.set_xlabel('Time (seconds)', fontsize=16)
ax1.set_ylabel('Achievable Accuracy (%)', fontsize=16)

# Grid for zoomed-in plot
ax1.grid(True, linestyle='--', linewidth=0.5)

# Tight layout and save
plt.tight_layout()
plt.savefig('time_to_accuracy_plot_vgg_with_zoom.pdf', format='pdf', bbox_inches='tight')
plt.show()



